/*
*  Copyright 2019-2025 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.transfer.rest;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.sequence.domain.Sequence;
import me.zhengjie.modules.sequence.service.SequenceService;
import me.zhengjie.modules.transfer.domain.TransferAc10;
import me.zhengjie.modules.transfer.domain.dto.TransferAc10QueryCriteria;
import me.zhengjie.modules.transfer.service.TransferAc10Service;
import me.zhengjie.modules.transfer.service.TransferOutService;
import me.zhengjie.tool.MyMap;
import me.zhengjie.tool.ValidateUtil;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author lzw
* @date 2025-05-26
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "api")
@RequestMapping("/api/transferOut")
public class TransferOutController {

    private final TransferOutService transferOutService;
    private final SequenceService sequenceService;

    @PostMapping("transferOutPreview")
    public ResponseEntity<Object> transferOutPreview(@Validated @RequestBody Map resources){



        MyMap<Object, Object> map = new MyMap<>();
        map.put("aac001",resources.get("aac001"));
        map.put("aab359","530111");
        map.put("aae011", SecurityUtils.getCurrentUsername());
        map.put("aic113",resources.get("aic113"));
        if(!ValidateUtil.isEmpty(resources.get("orginfoin"))){

            Map orginfoin = JSONObject.parseObject(resources.get("orginfoin").toString(), Map.class);

            if(ValidateUtil.isEmpty(orginfoin.get("aae270")) ){//预览的时候不一定有联系函编号
                Sequence sequence_aae270 = new Sequence();
                sequence_aae270.setName("aae270");
                orginfoin.put("aae270",sequenceService.nextSequence(sequence_aae270));
            }
            map.put("orginfoin", orginfoin);

        }


        return new ResponseEntity<>(transferOutService.transferOutPreview(map),HttpStatus.OK);
    }


    @PostMapping("transferOutSave")
    public ResponseEntity<Object> transferOutSave(@Validated @RequestBody Map resources) throws Exception{



        MyMap<Object, Object> map = new MyMap<>();
        map.put("aac001",resources.get("aac001"));
        map.put("aab359","530111");
        map.put("aae011", SecurityUtils.getCurrentUsername());
        map.put("aic113",resources.get("aic113"));

        Map orginfoin = JSONObject.parseObject(resources.get("orginfoin").toString(), Map.class);

        if(ValidateUtil.isEmpty(orginfoin.get("aae270")) ){//预览的时候不一定有联系函编号
            Sequence sequence_aae270 = new Sequence();
            sequence_aae270.setName("aae270");
            orginfoin.put("aae270",sequenceService.nextSequence(sequence_aae270));
        }
        map.put("orginfoin", orginfoin);
        return new ResponseEntity<>(transferOutService.transferOutSave(map),HttpStatus.OK);
    }

    /**
     * 转出登记撤销
     * @param resources
     * @return
     */
    @PostMapping("transferOutRevokeSave")
    public ResponseEntity<Object> transferOutRevokeSave(@Validated @RequestBody MyMap resources){


        return new ResponseEntity<>(transferOutService.transferOutRevokeSave(resources),HttpStatus.OK);
    }


}
